/*
 * Copyright (C) 2008 Matt Falkoski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.falko.android.raven.animator;

import java.util.HashMap;

import javax.microedition.khronos.opengles.GL10;

import com.falko.android.raven.renderer.Sprite2D;
import com.falko.android.raven.renderer.Texture;

public final class Animation {

	@SuppressWarnings("unused")
	private final String TAG = getClass().getName();

	private AnimationLogic logic_;
	private HashMap<String, Boolean> boolMap_;
	private HashMap<String, Integer> intMap_;
	private HashMap<String, Long> longMap_;
	private HashMap<String, Float> floatMap_;
	private HashMap<String, Texture> textureMap_;
	private HashMap<String, String> stringMap_;
	private Sprite2D sprites_[];

	public static final int SPRITE_COUNT = 50;

	public Animation() {

		boolMap_ = new HashMap<String, Boolean>();
		intMap_ = new HashMap<String, Integer>();
		longMap_ = new HashMap<String, Long>();
		floatMap_ = new HashMap<String, Float>();
		textureMap_ = new HashMap<String, Texture>();
		stringMap_ = new HashMap<String, String>();
		sprites_ = new Sprite2D[SPRITE_COUNT];
		for (int i = 0; i < SPRITE_COUNT; ++i) {
			sprites_[i] = new Sprite2D();
		}
	}

	public boolean getBool(String tag) {
		return boolMap_.get(tag);
	}

	public void putBool(String tag, boolean val) {
		boolMap_.put(tag, val);
	}

	public int getInt(String tag) {
		return intMap_.get(tag);
	}

	public void putInt(String tag, int val) {
		intMap_.put(tag, val);
	}
	
	public long getLong(String tag) {
		return longMap_.get(tag);
	}

	public void putLong(String tag, long val) {
		longMap_.put(tag, val);
	}

	public float getFloat(String tag) {
		return floatMap_.get(tag);
	}

	public void putFloat(String tag, float val) {
		floatMap_.put(tag, val);
	}

	public Texture getTexture(String tag) {
		return textureMap_.get(tag);
	}

	public void putTexture(String tag, Texture val) {
		textureMap_.put(tag, val);
	}

	public String getString(String tag) {
		return stringMap_.get(tag);
	}

	public void putString(String tag, String val) {
		stringMap_.put(tag, val);
	}

	public Sprite2D[] getSprites() {
		return sprites_;
	}

	public void bindLogic(AnimationLogic logic) {
		logic_ = logic;
		logic_.onInit(this);
	}

	public void onRecycle() {
		boolMap_.clear();
		intMap_.clear();
		longMap_.clear();
		floatMap_.clear();
		textureMap_.clear();
		stringMap_.clear();
		logic_ = null;
	}

	public void update() {
		logic_.update(this);
	}

	public boolean visible() {
		return logic_.visible(this);
	}

	public void render(GL10 gl) {
		logic_.render(this, gl);
	}

	public interface AnimationLogic {

		public abstract void update(Animation animation);

		public abstract void onInit(Animation animation);

		public abstract boolean visible(Animation animation);

		public abstract void render(Animation ainmation, GL10 gl);
	}

}
